package zpx.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class FtpApche {
    private static FTPClient ftpClient = new FTPClient();
    private static String encoding = System.getProperty("file.encoding");
    /**
     * Description: ��FTP�������ϴ��ļ�
     *
     * @Version1.0
     * @param url
     *            FTP������hostname
     * @param port
     *            FTP�������˿�
     * @param username
     *            FTP��¼�˺�
     * @param password
     *            FTP��¼����
     * @param path
     *            FTP����������Ŀ¼,����Ǹ�Ŀ¼��Ϊ��/��
     * @param filename
     *            �ϴ���FTP�������ϵ��ļ���
     * @param input
     *            �����ļ�������
     * @return �ɹ�����true�����򷵻�false
     */
    public static boolean uploadFile(String url, int port, String username,
            String password, String path, String filename, InputStream input) {
        boolean result = false;
 
        try {
            int reply;
            // ������Ĭ�϶˿ڣ�����ʹ��ftp.connect(url)�ķ�ʽֱ������FTP������
            ftpClient.connect(url);
            // ftp.connect(url, port);// ����FTP������
            // ��¼
            ftpClient.login(username, password);
            ftpClient.setControlEncoding(encoding);
            // �����Ƿ����ӳɹ�
            reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                System.out.println("����ʧ��");
                ftpClient.disconnect();
                return result;
            }
 
            // ת�ƹ���Ŀ¼��ָ��Ŀ¼��
            boolean change = ftpClient.changeWorkingDirectory(path);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            if (change) {
                result = ftpClient.storeFile(new String(filename.getBytes(encoding),"iso-8859-1"), input);
                if (result) {
                    System.out.println("�ϴ��ɹ�!");
                }
            }
            input.close();
            ftpClient.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }
 
    /**
     * �������ļ��ϴ���FTP��������
     *
     */
    public void testUpLoadFromDisk() {
        try {
            FileInputStream in = new FileInputStream(new File("E:/Ѹ������/��Ӱ/�¹���Ԫ��.rmvb"));
            boolean flag = uploadFile("192.168.1.102", 21, "plantynet","plantynet", "/", "����.rmvb", in);
            System.out.println(flag);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * Description: ��FTP�����������ļ�
     *
     * @Version1.0
     * @param url
     *            FTP������hostname
     * @param port
     *            FTP�������˿�
     * @param username
     *            FTP��¼�˺�
     * @param password
     *            FTP��¼����
     * @param remotePath
     *            FTP�������ϵ����·��
     * @param fileName
     *            Ҫ���ص��ļ���
     * @param localPath
     *            ���غ󱣴浽���ص�·��
     * @return
     */
    public static boolean downFile(String url, int port, String username,
            String password, String remotePath, String fileName,
            String localPath) {
        boolean result = false;
        try {
            int reply;
            ftpClient.setControlEncoding(encoding);
             
            /*
             *  Ϊ���ϴ������������ļ�����Щ�ط�����ʹ�������������
             *  new String(remotePath.getBytes(encoding),"iso-8859-1")ת�롣
             *  ������ԣ�ͨ����
             */
//            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
//            conf.setServerLanguageCode("zh");
 
            ftpClient.connect(url, port);
            // ������Ĭ�϶˿ڣ�����ʹ��ftp.connect(url)�ķ�ʽֱ������FTP������
            ftpClient.login(username, password);// ��¼
            // �����ļ���������Ϊ������
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            // ��ȡftp��¼Ӧ�����
            reply = ftpClient.getReplyCode();
            // ��֤�Ƿ��½�ɹ�
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
                System.err.println("FTP server refused connection.");
                return result;
            }
            // ת�Ƶ�FTP������Ŀ¼��ָ����Ŀ¼��
            ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
            // ��ȡ�ļ��б�
            FTPFile[] fs = ftpClient.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(fileName)) {
                    File localFile = new File(localPath + "/" + ff.getName());
                    OutputStream is = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }
 
            ftpClient.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }
 
    /**
     * ��FTP���������ļ����ص�����
     *
     */
    public void testDownFile() {
        try {
            boolean flag = downFile("127.0.0.1", 21, "zlb",
                    "123", "/", "����.txt", "D:/");
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
     
    public static void main(String[] args) {
        FtpApche fa = new FtpApche();
        fa.testUpLoadFromDisk();
    }
}